package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    void insert(Orders orders);
    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    @Select("select * from orders where status = #{paid} and order_time < #{localDateTime}")
    List<Orders> getByStatusAndOrderTime(Integer paid, LocalDateTime localDateTime);

    /**
     * 分页条件查询并按下单时间排序
     * @param ordersPageQueryDTO
     */
    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);


    /**
     * 根据id查询订单
     * @param id
     */
    @Select("select * from orders where id=#{id}")
    Orders getById(Long id);




    @Select("select orders.amount from orders where orders.order_time > #{beginTime} and order_time < #{endTime} and status = 5 group by orders.id;")
    List<BigDecimal> getByDate(LocalDateTime beginTime, LocalDateTime endTime);

    @Select("select count(*) from orders where orders.order_time > #{beginTime} and order_time < #{endTime} and status = 5;")
    Integer getByDate1(LocalDateTime beginTime, LocalDateTime endTime);



    Integer getByCreateTime(Map map);


    @Select("select count(*) from orders where order_time > #{beginTime} and order_time < #{endTime}")
    Integer getByDate2(LocalDateTime beginTime, LocalDateTime endTime);



    @Select("select name from order_detail join sky_take_out.orders o on o.id = order_detail.order_id where order_time > #{beginTime} and order_time < #{endTime} group by name order by count(*) desc limit 10")
    List<String> getTop10NameList(LocalDateTime beginTime, LocalDateTime endTime);


    @Select("select count(*) from order_detail join sky_take_out.orders o on o.id = order_detail.order_id where order_time > #{beginTime} and order_time < #{endTime} group by name order by count(*) desc limit 10")
    List<String> getTop10NumberList(LocalDateTime beginTime, LocalDateTime endTime);

    @Select("select count(id) from orders where status = #{status}")
    Integer countStatus(Integer status);

    /**
     * 根据动态条件统计营业额数据
     * @param map
     * @return
     */
    Double sumByMap(Map map);

    /**
     * 根据动态条件统计订单数量
     * @param map
     * @return
     */
    Integer countByMap(Map map);
}
